package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Employee;
import com.ruoyi.system.domain.WageSlipInfo;
import com.ruoyi.system.domain.vo.WageSlipInfoVo;
import com.ruoyi.system.service.IEmployeeService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.IWageSlipInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * 工资条信息Controller
 *
 * @author 管鸿影
 * @date 2024-12-08
 */
@Controller
@RequestMapping("/system/info")
public class WageSlipInfoController extends BaseController {
    private String prefix = "system/info";

    @Autowired
    private IWageSlipInfoService wageSlipInfoService;
    @Resource
    private IEmployeeService employeeService;

    @Resource
    private ISysRoleService roleService;
    @RequiresPermissions("system:info:view")
    @GetMapping()
    public String info() {
        return prefix + "/info";
    }



    /**
     * 查询工资条信息列表
     */
    @RequiresPermissions("system:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WageSlipInfo wageSlipInfo) {
        startPage();
        Long userId = ShiroUtils.getSysUser().getUserId();
        SysUser sysUser = ShiroUtils.getSysUser();
        Set<String> strings = roleService.selectRoleKeys(userId);
        if (!sysUser.isAdmin() || !strings.contains("admin2")){
            wageSlipInfo.setEmployeeId(sysUser.getEmpId());
        }
        List<WageSlipInfo> list = wageSlipInfoService.selectWageSlipInfoList(wageSlipInfo);
        List<WageSlipInfoVo> wageSlipInfoVos = new ArrayList<>();
        list.forEach(ws -> {
            Employee employee = employeeService.selectEmployeeByEmployeeId(ws.getEmployeeId());
            WageSlipInfoVo wageSlipInfoVo = new WageSlipInfoVo();
            BeanUtils.copyProperties(ws,wageSlipInfoVo);
            wageSlipInfoVo.setEmployee(Optional.ofNullable(employee).map(Employee::getName).orElse("关联员工已被删除..."));
            wageSlipInfoVos.add(wageSlipInfoVo);
        });
        return getDataTable(wageSlipInfoVos);
    }

    /**
     * 导出工资条信息列表
     */
    @RequiresPermissions("system:info:export")
    @Log(title = "工资条信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WageSlipInfo wageSlipInfo) {
        List<WageSlipInfo> list = wageSlipInfoService.selectWageSlipInfoList(wageSlipInfo);
        ExcelUtil<WageSlipInfo> util = new ExcelUtil<WageSlipInfo>(WageSlipInfo.class);
        return util.exportExcel(list, "工资条信息数据");
    }

    /**
     * 新增工资条信息
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        List<Employee> employees = employeeService.selectEmployeeList(new Employee());
        modelMap.put("employees", employees);
        return prefix + "/add";
    }

    /**
     * 新增保存工资条信息
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "工资条信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WageSlipInfo wageSlipInfo) {
        return toAjax(wageSlipInfoService.insertWageSlipInfo(wageSlipInfo));
    }

    /**
     * 修改工资条信息
     */
    @RequiresPermissions("system:info:edit")
    @GetMapping("/edit/{wageSlipId}")
    public String edit(@PathVariable("wageSlipId") Long wageSlipId, ModelMap mmap) {
        WageSlipInfo wageSlipInfo = wageSlipInfoService.selectWageSlipInfoByWageSlipId(wageSlipId);
        mmap.put("wageSlipInfo", wageSlipInfo);
        List<Employee> employees = employeeService.selectEmployeeList(new Employee());
        mmap.put("employees", employees);
        return prefix + "/edit";
    }

    /**
     * 修改保存工资条信息
     */
    @RequiresPermissions("system:info:edit")
    @Log(title = "工资条信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WageSlipInfo wageSlipInfo) {
        return toAjax(wageSlipInfoService.updateWageSlipInfo(wageSlipInfo));
    }

    /**
     * 删除工资条信息
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "工资条信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wageSlipInfoService.deleteWageSlipInfoByWageSlipIds(ids));
    }
}
